博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android中文乱码解决大全
阅读量:7070 次
发布时间:2019-06-28

本文共 5730 字,大约阅读时间需要 19 分钟。

最近在做个MP3播放器,出现中文乱码问题,在网上找了很多解决办法,我整理了出现乱码的点和解决方案,拿出来和大家共享一下

  1.读取中文文件乱码解决方法

 

package com.apj.conv;      import java.io.BufferedInputStream;   import java.io.BufferedReader;   import java.io.File;   import java.io.FileInputStream;   import java.io.FileNotFoundException;   import java.io.IOException;   import java.io.InputStreamReader;      import android.app.Activity;   import android.os.Bundle;   import android.os.Environment;   import android.widget.TextView;      public class ConverActivity extends Activity {               private TextView textview ;                       @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                       textview = (TextView) findViewById(R.id.lrctext);                                 System.out.println("===================convertCodeAndGetText begin=================== ");           ///获得SDCard中文件的路径           String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator ;           String tochinese = convertCodeAndGetText(path+"a.txt");           System.out.println(tochinese);           System.out.println("===================cconvertCodeAndGetText end===================");           textview.setText(tochinese);                   }              public String convertCodeAndGetText(String str_filepath) {
// ת�� File file = new File(str_filepath); BufferedReader reader; String text = ""; try { FileInputStream fis = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fis); in.mark(4); byte[] first3bytes = new byte[3]; in.read(first3bytes); in.reset(); if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {
// utf-8 reader = new BufferedReader(new InputStreamReader(in, "utf-8")); } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) { reader = new BufferedReader( new InputStreamReader(in, "unicode")); } else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(in, "utf-16be")); } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(in, "utf-16le")); } else { reader = new BufferedReader(new InputStreamReader(in, "GBK")); } String str = reader.readLine(); while (str != null) { text = text + str + "\n"; str = reader.readLine(); } reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return text; } }

2. 连接网络读取文件内容中文乱码解决办法

URL myFileUrl = null;         myFileUrl = new URL(url);      HttpURLConnection conn;      conn = (HttpURLConnection) myFileUrl.openConnection();      conn.setDoInput(true);      conn.connect();      InputStream is = conn.getInputStream();      BufferedReader br = new BufferedReader(new InputStreamReader(is,        "GB2312"));      sb = new StringBuffer();      String data = "";      while ((data = br.readLine()) != null) {              sb.append(data+"\n");         }         String result = sb.toString();

3.读取网络文件中文名下载乱码解决办法

  1).先在设置服务器编码:找到Tomcat安装目录下的server.xml文件(Tomcat 6.0\conf\server.xml)。设置编码为UTF-8
  <Connectorport="8080" URIEncoding="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>
  2). android 中代码为:

try {            lrcUrl = "http://192.168.0.214/vote/mp3/" + URLEncoder.encode("中文.mp3","UTF-8");           } catch (UnsupportedEncodingException e) {               // TODO Auto-generated catch block               e.printStackTrace();           }              int result1 = downFile(lrcUrl, "mp3/", "中文.mp3");   **    * 该函数返回整型( -1:代表下载文件出错 ;0:代表下载成功;1:代表文件已存在)    **/   public int downFile(String urlStr, String path, String fileName) {       InputStream inputStream = null;       try {           FileUtils fileUtils = new FileUtils();           if (fileUtils.isFileExist( fileName,path )) {               return 1;           } else {               inputStream = getInputStreamFromUrl(urlStr);               File resultFile = fileUtils.write2SDFromInput(path, fileName,                       inputStream);               if (resultFile == null) {                   return -1;               }              }       } catch (Exception e) {           // TODO: handle exception           e.printStackTrace();           return -1;       } finally {           try {               inputStream.close();           } catch (Exception e2) {               e2.printStackTrace();           }       }       return 0;   }      /**   * 根据URL得到输入流   *    * @param urlStr   * @return   * @throws MalformedURLException   * @throws IOException   */     public InputStream getInputStreamFromUrl(String urlStr)           throws MalformedURLException, IOException {       url = new URL(urlStr);       HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();       InputStream inputStream = urlConn.getInputStream();       return inputStream;   }

 

 

 

转载地址:http://cphll.baihongyu.com/

你可能感兴趣的文章
[BZOJ1030][JSOI2007]文本生成器(AC自动机+DP)
查看>>
如何判断元素是否在当前文档显示区内?
查看>>
ICMP协议
查看>>
IOS 5 ARC机制 (四)
查看>>
生成二维码
查看>>
网页打开时左上角带的小图标
查看>>
java中json数据生成和解析(复杂对象演示)
查看>>
二分 三分搜索
查看>>
C#中反射type记录
查看>>
View在测量时的MeasureSpec由什么决定?
查看>>
HDU3067 小t的游戏
查看>>
JDK源码 ArrayList
查看>>
程序员面试大揭秘——应聘微软、亚马逊、谷歌、苹果等IT公司你都要做什么准备?...
查看>>
【转】如何理解云计算?很简单,就像吃货想吃披萨了
查看>>
ECharts测量图,功率图
查看>>
个人总结作业
查看>>
C++的预处理(Preprocess)
查看>>
仿网易菜单 实现侧滑 SlidingMenu
查看>>
延时显示的三种实现方式
查看>>
LeetCode算法题-Missing Number(Java实现-四种解法)
查看>>